package com.cloud.check.interfaces;

import com.cloud.check.api.bean.HumanDetail;
import com.cloud.check.api.bean.RecCheckUnitCriteriaBean;
import com.cloud.check.api.bean.SuperviseBean;
import com.cloud.check.common.config.FeignConfiguration;
import com.cloud.check.common.result.SelectItem;
import com.github.pagehelper.PageInfo;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.List;

@FeignClient(value = "SYSTEM",  configuration = FeignConfiguration.class, fallbackFactory = FeignClientSystemFallbackFactory.class)
public interface FeignClientSystem {

    /**
     * 获取当前登录机构领域信息
     * @param unitId
     * @return
     */
    @RequestMapping(value = "system/interface/domains/load", method = RequestMethod.GET)
    List<SelectItem> loadUnitDomains(@RequestParam("unitId")long unitId);

    /**
     * 获取字典项
     * @param sysId 子系统编码
     * @param dictKey 字典键值
     * @return
     */
    @RequestMapping(value = "system/interface/sysdict/listdicts", method = RequestMethod.GET)
    List<SelectItem> loadDictItems(@RequestParam("sysId") Long sysId, @RequestParam("dictKey") String dictKey);

    /**
     * 获取地址信息
     * @return
     */
    @RequestMapping(value="system/interface/address/load", method = RequestMethod.GET)
    List<SelectItem> loadAddressItems();

    /**
     * 加载部门人员
     * @return
     */
    @RequestMapping(value="system/interface/human/load", method = RequestMethod.GET)
    List<SelectItem> loadHuman(@RequestParam("unitId")Long unitId);

    /**
     * 当前部门领域
     * @param unitId
     * @return
     */
    @RequestMapping(value = "system/interface/domains/loadUnitDomainsByUnitId", method = RequestMethod.GET)
    List<SelectItem> loadUnitDomainsByUnitId(@RequestParam("unitId")long unitId);

    /**
     * 获取当前登录机构用户基本信息
     */
    @RequestMapping(value = "system/interface/humanunit/detail", method = RequestMethod.GET)
    HumanDetail getHumanDetail(@RequestParam("humanId")long humanId);

    /**
     * 获取当前机构检查信息
     */
    @RequestMapping(value = "system/interface/search/checkitem", method = RequestMethod.POST)
    PageInfo<SuperviseBean> searchCheckItem(@RequestBody RecCheckUnitCriteriaBean recCheckUnitCriteriaBean);

    /**
     * 查询当前登陆者的所属主体领域
     * @param unitId 机构
     * @return
     */
    @RequestMapping(value = "system/interface/unitInfo/findSubjectDomain", method = RequestMethod.GET)
    List<SelectItem> findSubjectDomain(@RequestParam("unitId")long unitId);
}

@Component
class FeignClientSystemFallbackFactory implements FallbackFactory<FeignClientSystem> {
    private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientSystemFallbackFactory.class);
    @Override
    public FeignClientSystem create(final Throwable cause) {
        return new FeignClientSystem() {

            @Override
            public List<SelectItem> loadUnitDomains(@RequestParam("unitId")long unitId) {
                if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
                    FeignClientSystemFallbackFactory.LOGGER.debug("loadAddressItems fallback; reason was:" + cause);
                }
                return Collections.emptyList();
            }

            @Override
            public List<SelectItem> loadDictItems(@RequestParam("sysId") Long sysId, @RequestParam("dictKey") String dictKey) {
                if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
                    FeignClientSystemFallbackFactory.LOGGER.debug("loadAddressItems fallback; reason was:" + cause);
                }
                return Collections.emptyList();
            }

            @Override
            public List<SelectItem> loadAddressItems() {
                if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
                    FeignClientSystemFallbackFactory.LOGGER.debug("loadAddressItems fallback; reason was:" + cause);
                }
                return Collections.emptyList();
            }

            @Override
            public List<SelectItem> loadHuman(Long unitId) {
                if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
                    FeignClientSystemFallbackFactory.LOGGER.debug("loadAddressItems fallback; reason was:" + cause);
                }
                return Collections.emptyList();
            }

            @Override
            public List<SelectItem> loadUnitDomainsByUnitId(long unitId) {
                return null;
            }

            @Override
            public PageInfo<SuperviseBean> searchCheckItem(RecCheckUnitCriteriaBean recCheckUnitCriteriaBean) {
                if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
                    FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
                }
                return null;
            }

            @Override
            public List<SelectItem> findSubjectDomain(long unitId) {
                return null;
            }

            @Override
            public HumanDetail getHumanDetail(long humanId) {
                if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
                    FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
                }
                return null;
            }
        };
    }

}